{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quantum Entanglement with Classiq\n",
    "\n",
    "Entanglement is an important aspect to study the quantum algorithms. In this tutorial, we show how to create a bell pair state $|\\Phi^{+}\\rangle$ of 2 qubits, using the Hadamard and the Controlled-NOT transformation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/5134159f-5b5d-4f36-9242-584b79303722?version=0.42.0\n"
     ]
    }
   ],
   "source": [
    "from classiq import *\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def my_bell_state(reg: QArray[QBit]) -> None:\n",
    "    H(reg[0])\n",
    "    CX(reg[0], reg[1])\n",
    "\n",
    "@qfunc\n",
    "def main(registers: Output[QArray[QBit]]) -> None:\n",
    "    allocate(2, registers)\n",
    "    my_bell_state(registers)\n",
    "\n",
    "model = create_model(main)\n",
    "write_qmod(model, \"bell_state_of_2_qubits\")\n",
    "\n",
    "\n",
    "qprog = synthesize(model)\n",
    "\n",
    "circuit = QuantumProgram.from_qprog(qprog)\n",
    "\n",
    "circuit.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Mathematical Background\n",
    "\n",
    "Alice has a qubit which is initially set to $|0\\rangle$<br>\n",
    "Bob has a qubit which is initally set to $|0\\rangle$\n",
    "\n",
    "Alice applies 2x2 Hadamard Matrix (H) to create a superposition of her qubit's state. It is defined as:<br>\n",
    "\n",
    "$$H \\cdot \\begin{pmatrix} 1 \\\\ 0 \\end{pmatrix} = \\begin{pmatrix} \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}} \\end{pmatrix} \\cdot \\begin{pmatrix} 1 \\\\ 0 \\end{pmatrix} = \\begin{pmatrix} \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{2}} \\end{pmatrix} = \\frac{1}{\\sqrt{2}} \\left| 0 \\right\\rangle + \\frac{1}{\\sqrt{2}} \\left| 1 \\right\\rangle$$\n",
    "\n",
    "Alice and Bob combine their qubits and generate the composite quantum state as:\n",
    "$$\\begin{pmatrix} \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{2}} \\end{pmatrix} \\otimes \\begin{pmatrix} 1 \\\\ 0 \\end{pmatrix} = \\begin{pmatrix} \\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ \\frac{1}{\\sqrt{2}} \\\\ 0 \\end{pmatrix}$$\n",
    "\n",
    "Alice now applies the Controlled NOT gate operation, with her qubit being the control qubit and Bob's qubit as the target qubit. The Controlled NOT qubit when applied only affects the target qubit by inverting its state if the control qubit is $|1\\rangle$. The 4x4 matrix for Controlled NOT(CNOT) transformation matrix is defined as: <br>\n",
    "$$\\text{CNOT} = \\begin{pmatrix}\n",
    "1 & 0 & 0 & 0 \\\\\n",
    "0 & 1 & 0 & 0 \\\\\n",
    "0 & 0 & 0 & 1 \\\\\n",
    "0 & 0 & 1 & 0\n",
    "\\end{pmatrix}$$\n",
    "The new quantum state is:\n",
    "$$\\begin{pmatrix} 1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0 \\end{pmatrix} \\begin{pmatrix} \\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ \\frac{1}{\\sqrt{2}}\\\\ 0 \\end{pmatrix} = \\begin{pmatrix} \\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ 0 \\\\ \\frac{1}{\\sqrt{2}} \\end{pmatrix} = \\frac{1}{\\sqrt{2}} \\left| 00 \\right\\rangle + \\frac{1}{\\sqrt{2}} \\left| 11 \\right\\rangle$$\n",
    "\n",
    "In this situation the qubit's of Alice and Bob are correlated to each other.<br>\n",
    "If we measure both the qubits we will either get the state $|00\\rangle$ or the state $|11\\rangle$ with equal probability.\n",
    "\n",
    "It is described as:\n",
    "- When Alice oberseve her state as $|0\\rangle$ then the state of Bob's qubit collapses to the state $|0\\rangle$\n",
    "- When Alice oberseve her state as $|1\\rangle$ then the state of Bob's qubit collapses to the state $|1\\rangle$ \n",
    "\n",
    "The resultant entangled state is designated as a bell pair state $|\\Phi^{+}\\rangle$\n",
    "$$|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\left( |00\\rangle + |11\\rangle \\right)$$\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
